package com.ljh.SimpleTradingSystem.controller;

import com.ljh.SimpleTradingSystem.mybatis.entity.Auction;
import com.ljh.SimpleTradingSystem.mybatis.mapper.AuctionMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/auction")
public class AuctionController {
    @Resource
    AuctionMapper auctionMapper;

    @GetMapping
    @ResponseBody
    public List<Auction> getAuction(@RequestParam int id) {
        List<Auction> auctionList = auctionMapper.selectByGoodsId(id);
        return auctionList;
    }

    @GetMapping("/maxprice")
    @ResponseBody
    public String getMaxPrice(@RequestParam int id) {
        Auction auction = auctionMapper.getMaxPriceByGoodsId(id);
        if (auction == null) return "null";
        return String.valueOf(auction.getPrice());
    }

    @PostMapping
    @ResponseBody
    public String addAuction(@RequestParam int goodsId, @RequestParam String userName,
                             @RequestParam double price) {
        Date time = new Date();
        String auctionTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time);

        Auction auction = new Auction(goodsId, userName, price, auctionTime);
        auctionMapper.insert(auction);
        return "success";
    }
}
